package com.localization;
import android.os.Message;
import android.util.Log;
import com.example.myprojectoffeixue.Extra;
import com.example.myprojectoffeixue.GateWay;
import com.example.myprojectoffeixue.MyApplication;
import com.example.myprojectoffeixue.SheBeiClass;
import java.util.ArrayList;
import java.util.HashMap;


/**
 * Created by Edward on 2017/1/7.
 */
public class SocketCmdsDistribute implements GlobalDefine {
public static ArrayList<GateWay> m_gateway=new ArrayList<>();//从本地或者从http接口获取的设备列表
public static HashMap<String ,ArrayList<SheBeiClass>> m2_gateway=new HashMap<String, ArrayList<SheBeiClass>>();//从内网获取的设备列表
    /*命令分发及功能解析*/
    public static void CmdsDistribute(HostCmd cmds) {
        Log.d("CMD From Gateway:",Utils.printHexString(cmds.getmHostCmd(),cmds.getLength()));
        int SOC = cmds.get8BitVal();
        int length = cmds.get16BitVal();
        int tp = cmds.get16BitVal();
        byte[] mac = cmds.getStringVal(6);
        String h = bytesToHexString(mac);
        Log.d("响应的MAC",h);
        int flag = 0;
        for (int i = 0;i < m_gateway.size(); i++) {
            if (h.equals(m_gateway.get(i).mac)) {
                flag = 1;
            }
        }
        if (flag == 0) {
            Log.d("11111111111111111","11111111111111111"+"--"+m_gateway.size());
            return;
        }else {
            Log.d("11111111111111111","222222222222222222");
            int dir = cmds.get8BitVal();
            int opcode = cmds.get16BitVal();
        /*屏蔽命令*/
            if (dir != 0x02 && dir != 0x03)
                return;
            switch (opcode & 0xff00) {
                case GATEWAY_UDP_FUNCTION: {
                    Log.d("udp解析", "udp");
                    gateway_udp(h, opcode, cmds);
                }
                break;

                default:
                    receive(Utils.printHexString(cmds.getmHostCmd(), cmds.getLength()));
                    System.out.println("This cmd Unporcess " + Utils.IntToHexString(opcode));
                    break;
            }

        }
    }

    /*UD广播包解析*/
    public static void gateway_udp(String MAC, int opcode, HostCmd cmds) {
        switch (opcode) {
            case GATEWAY_UDP_REQUEST: {
                int iplen = cmds.get8BitVal();
                byte[] ip = cmds.getStringVal(iplen);
                int port = cmds.get16BitVal();
                System.out.println("MAC:" + new String(ip));
                System.out.println("port:" + port);
                GlobalValue.tcpclient = new SocketTcpClient(GlobalValue.mGateway);
                GlobalValue.tcpclient.connect(MAC, new String(ip), port);
            }
            break;
            default:
                Log.d("cmd", Integer.toHexString(opcode) + "");
                System.out.println("This cmd Unporcess " + Integer.toHexString(opcode));
                break;
        }
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static void receive(String msg) {
        if (msg.length() >= 22) {
            // 产品的序号
            // String tp = msg.substring(0, 4);
            String mac = msg.substring(10, 22);

            String dir = msg.substring(22, 24);

            String d0 = msg.substring(24, 26);

            String d1 = msg.substring(26, 28);

            System.out.print("mac：[" + mac + "] dir：[" + dir + "] d0：[" + d0 + "] d1：[" + d1 + "]");

            // 所有当前在线的设备的节点信息（心跳包）
            if ("03".equals(dir) && "01".equals(d0) && "02".equals(d1)) {
                int DeviceSize = Integer.parseInt(msg.substring(28, 30), 16);
                ArrayList<SheBeiClass>devices=new ArrayList<SheBeiClass>();
                for(int i=0;i<DeviceSize;i++){
                    SheBeiClass sb=new SheBeiClass();
                    sb.ieee=msg.substring(30+i*24,46+i*24);
                    Log.d("ieee",sb.ieee);
                    sb.port=msg.substring(46+i*24,48+i*24);
                    Log.d("port",sb.port);
                    sb.deviceId=msg.substring(48+i*24,52+i*24);
                    Log.d("deviceId",sb.deviceId);
                   devices.add(sb);
                }
                m2_gateway.put(mac,devices);
            }
            // 开关量节点的开关状态变化上报
            else if ("03".equals(dir) && "10".equals(d0) && "02".equals(d1)) {
                String ieee=msg.substring(28,44);
                String port=msg.substring(44,46);
                String status=msg.substring(46,48);
                for(int i=0;i<MyApplication.lsb.size();i++){
                    if (ieee.equals(MyApplication.lsb.get(i).ieee) && (port.equals(MyApplication.lsb.get(i).port))) {

                        Log.d("开关上报","开关上报");
                        MyApplication.lsb.get(i).status = status;
                        if(MyApplication.hand!=null){
                            Message msg2=new Message();
                            msg2.what = 0;
                            msg2.obj = "1007";
                            Log.d("发送消息","发送");
                           MyApplication.hand.sendMessage(msg2);
                        }
                    }
                }
                for(int i=0;i<MyApplication.lsb8.size();i++){
                    if (ieee.equals(MyApplication.lsb8.get(i).ieee) && (port.equals(MyApplication.lsb8.get(i).port))) {

                        Log.d("开关上报","开关上报");
                        MyApplication.lsb8.get(i).status = status;
                        if(MyApplication.hand!=null){
                            Message msg2=new Message();
                            msg2.what = 0;
                            msg2.obj = "1007";
                            Log.d("发送消息","发送");
                            MyApplication.hand.sendMessage(msg2);
                        }
                    }
                }








            }

            // 查询开关量节点当前的状态命令应答
            else if ("02".equals(dir) && "10".equals(d0) && "03".equals(d1)) {
                Log.d("查询开关设备", "查询包");
            }
            // 控制门锁节点的开关命令应答
            else if ("02".equals(dir) && "40".equals(d0) && "01".equals(d1)) {
                Log.d("门锁应答", "门锁");
            }
            // 控制门锁节点的开关命令应答
            else if ("03".equals(dir) && "40".equals(d0) && "02".equals(d1)) {
                Log.d("门锁应答", "门锁");
                String ieee=msg.substring(28,44);
                String port=msg.substring(44,46);
                String status=msg.substring(46,48);
                for(int i=0;i<MyApplication.lsb1.size();i++){
                    if (ieee.equals(MyApplication.lsb1.get(i).ieee) && (port.equals(MyApplication.lsb1.get(i).port))) {

                        Log.d("开关上报","门锁上报"+status);
                        MyApplication.lsb1.get(i).status = status;
                        if(MyApplication.hand!=null){
                            Message msg2=new Message();
                            msg2.what = 0;
                            msg2.obj = "1005";
                            Log.d("发送消息","发送");
                            MyApplication.hand.sendMessage(msg2);
                        }
                    }
                }
            }
            // 门锁节点的电量值上报
            else if ("03".equals(dir) && "40".equals(d0) && "03".equals(d1)) {
                Log.d("门锁应答", "门锁");
            }
            // 查询门锁的电量信息反馈
            else if ("02".equals(dir) && "40".equals(d0) && "04".equals(d1)) {
                Log.d("门锁应答", "门锁");
            }
            // 获取力维门锁信息应答
            else if ("02".equals(dir) && "40".equals(d0) && "10".equals(d1)) {
                Log.d("门锁应答", "门锁");
            }
            // 获取力维门锁电量状态上报

            else if ("03".equals(dir) && "40".equals(d0) && "12".equals(d1)) {
                Log.d("门锁应答", "门锁");
            }
            // 力维实时开门记录上报
            else if ("03".equals(dir) && "40".equals(d0) && "13".equals(d1)) {
                Log.d("门锁应答", "门锁");
            }
            // 力维非法操作命令上报
            else if ("03".equals(dir) && "40".equals(d0) && "14".equals(d1)) {
                Log.d("门锁应答", "门锁");
            }
            // 用户管理操作结果上报
            else if ("03".equals(dir) && "40".equals(d0) && "19".equals(d1)) {
                Log.d("门锁应答", "门锁");
            }
            // 启用报警器设备属性值设置命令应答
            else if ("02".equals(dir) && "30".equals(d0) && "01".equals(d1)) {
                Log.d("传感器包", "传感器包");
            }
            else if ("03".equals(dir) && "30".equals(d0) && "02".equals(d1)) {
                Log.d("传感器包", "传感器包");
                String ieee=msg.substring(28,44);
                String port=msg.substring(44,46);
                String status=msg.substring(50,52);
                for(int i=0;i<MyApplication.lsb4.size();i++){
                    if (ieee.equals(MyApplication.lsb4.get(i).ieee) && (port.equals(MyApplication.lsb4.get(i).port))) {
                        Log.d("开关上报","门磁上报"+status+"--"+ieee+"--"+port);
                        MyApplication.lsb4.get(i).status = status;
                        if(MyApplication.hand!=null){
                            Message msg2=new Message();
                            msg2.what = 0;
                            msg2.obj = "1009";
                            Log.d("发送消息","发送");
                            MyApplication.hand.sendMessage(msg2);
                        }
                    }
                }
                for(int i=0;i<MyApplication.lsb2.size();i++){
                    if (ieee.equals(MyApplication.lsb2.get(i).ieee) && (port.equals(MyApplication.lsb2.get(i).port))) {
                        MyApplication.lsb2.get(i).status = status;
                        if(MyApplication.hand!=null){
                            MyApplication.hand.sendEmptyMessage(60);
                        }
                    }
                }
            }
            // 启用/禁用报警器设备状态改变上报
            else if ("03".equals(dir) && "30".equals(d0) && "04".equals(d1)) {
                Log.d("传感器包", "传感器包");
                String ieee=msg.substring(28,44);
                String port=msg.substring(44,46);
                String status=msg.substring(50,52);
                for(int i=0;i<MyApplication.lsb2.size();i++){
                    if (ieee.equals(MyApplication.lsb2.get(i).ieee) && (port.equals(MyApplication.lsb2.get(i).port))) {
                        Log.d("开关上报","红外上报"+status+"--"+ieee+"--"+port);
                        MyApplication.lsb2.get(i).status = status;
                        if(MyApplication.hand!=null){
                            Message msg2=new Message();
                            msg2.what = 0;
                            msg2.obj = "1010";
                            Log.d("发送消息","发送");
                            MyApplication.hand.sendMessage(msg2);
                        }


                    }
                }
            }
            // 控制取电开关节点的命令应答
            else if ("02".equals(dir) && "41".equals(d0) && "01".equals(d1)) {
                Log.d("取电", "取电包");
            } else if ("03".equals(dir) && "41".equals(d0)) {
                if ("02".equals(d1)) {
                    String ieee=msg.substring(28,44);
                    String port=msg.substring(44,46);
                    String status=msg.substring(46,48);

                    Log.d("反馈",ieee+"--"+port+"--"+status);
                    for(int i=0;i<MyApplication.lsb5.size();i++){
                        if (ieee.equals(MyApplication.lsb5.get(i).ieee) && (port.equals(MyApplication.lsb5.get(i).port))) {

                            Log.d("开关上报","开关上报");
                            MyApplication.lsb5.get(i).status = status;
                            if(MyApplication.hand!=null){
                                Message msg2=new Message();
                                msg2.what = 0;
                                msg2.obj = "1006";
                                Log.d("发送消息","发送");
                                MyApplication.hand.sendMessage(msg2);
                            }
                        }
                    }
                    // 控制取电开关节点的状态变化上报
                    Log.d("取电", "取电包");
                } else if ("03".equals(d1)) {
                    // 电量使用值上报
                    String ieee=msg.substring(28,44);
                    String port=msg.substring(44,46);
                    int power=Integer.parseInt(msg.substring(46,54),16);
                    Log.d("取电端口",ieee+"--"+port+power);
                    Message msg2 = new Message();
                    msg2.obj = power+"";
                    msg2.what = 3;//获取取电开关电量成功反馈
                    if (MyApplication.hand != null) {
                       MyApplication.hand.sendMessage(msg2);
                    }















                }
            }
            // 中央空调状态查询反馈
            else if ("03".equals(dir) && "42".equals(d0) && "01".equals(d1)) {
                Log.d("中央空调", "中央空调包");
            } else if ("03".equals(dir) && "42".equals(d0) && "02".equals(d1)) {

               // FC001D0001
                // 202020202054
                // 034202
                // 00124B0009F2D31E080101031E120000000043






              /*  String ieee = jb.getString("ieee");
                String port = jb.getString("port");
                String Key = ieee + port;
                JSONObject n = jb.getJSONObject("extra");
                t = JSON.parseObject(n.toString(), Extra.class);
                kt_map.put(Key, t);
                if (hand != null) {
                    hand.sendEmptyMessage(32);
                }*/

                Extra et = new Extra();
                String ieee=msg.substring(28,44);
                String port=msg.substring(44,46);
                String OnorOff=msg.substring(46,48);
                String mode=msg.substring(48,50);
                String speed=msg.substring(50,52);
                int temperature=Integer.parseInt(msg.substring(52,54),16);
                int housetemperature=Integer.parseInt(msg.substring(54,56),16);
                String timerStatus=msg.substring(56,58);
                int timerHour;
                int timerMin;

                if("00".equals(timerStatus)){
                     et.timerType="00";
                     timerHour=Integer.parseInt(msg.substring(58,60),16);
                     timerMin=Integer.parseInt(msg.substring(60,62),16);}else{
                     String timerType=msg.substring(58,60);
                     timerHour=Integer.parseInt(msg.substring(60,62),16);
                     timerMin=Integer.parseInt(msg.substring(62,64),16);
                     et.timerType=timerType;
                }
                String Key = ieee + port;
                et.timerStatus=timerStatus;
                et.mode=mode;
                et.powerStatus=OnorOff;
                et.roomTemperature=housetemperature+"";
                et.temperature=temperature+"";
                et.speed=speed;
                et.timerMin=timerMin+"";
                et.timerHour=timerHour+"";
                MyApplication.t=et;
                MyApplication.kt_map.put(Key, et);
                if (MyApplication.hand != null) {
                    MyApplication.hand.sendEmptyMessage(32);
                }
                Log.d("空调包",OnorOff+"--"+mode+"--"+speed+"--"+temperature+"--"+housetemperature+"--"+timerStatus+"--"+timerHour+"--"+timerMin);

            } else if ("03".equals(dir) && "42".equals(d0) && "03".equals(d1)) {
                Log.d("中央空调", "中央空调包");
            }





            // 红外转发器
            else if ("02".equals(dir) && "12".equals(d0)) {
                if ("19".equals(d1)) {
                    // 1220H,1221H,1223H,1225H应答
                    Log.d("红外转发器", "红外转发器包");

                }
            } else if ("03".equals(dir) && "12".equals(d0)) {
                // 红外学习完成数据上报(学习模式学习到的数据)
                if ("24".equals(d1)) {
                    String YuanMa=msg.substring(50,510);
                    Log.d("Yuanma",YuanMa);
                   if (MyApplication.AnNiuId != -1) {
                       MyApplication.YuanSM.put(MyApplication.AnNiuId, YuanMa);
                        if (MyApplication.hand != null) {
                           MyApplication.hand.sendEmptyMessage(2);
                            //获取按键源码成功反馈
                        }
                    }
                }
                if ("26".equals(d1)) {
                    MyApplication.MK.clear();
                    String ieee=msg.substring(28,44);
                    String port=msg.substring(44,46);
                    int data=Integer.parseInt(msg.substring(48,52),16);

                    for(int i=0;i<data/2;i++){
                        String codeindex=""+Integer.parseInt(msg.substring(52+i*4,56+i*4),16);
                        MyApplication.MK.add(codeindex);
                    }
                    if (MyApplication.hand != null) {
                        MyApplication.hand.sendEmptyMessage(1);
                        //获取码库列表成功反馈
                    }
                    Log.d("result",ieee+"---"+port+"--"+data);
                }
            }

            // 系统应答
            else if ("02".equals(dir) && "00".equals(d0)) {
                if ("09".equals(d1)) {
                    // 重启应答
                    Log.d("系统包", "系统包");
                } else if ("10".equals(d1)) {
                    // 开放网络应答
                    Log.d("系统包", "系统包");
                } else if ("11".equals(d1)) {
                    // 关闭网络应答
                    Log.d("系统包", "系统包");
                }
            } else if ("03".equals(dir) && "00".equals(d0) && "FF".equals(d1)) {
                // 设备恢复出厂设置
                Log.d("系统包", "系统包");
            }

        }
    }

}
